<?php

namespace App\Admin\Controllers;


use App\Admin\Actions\Grid\RowActions\AuditRemarkAction;
use App\Admin\Actions\Grid\RowActions\ExpressAction;
use App\Admin\Actions\Grid\RowActions\UploadAgreementAction;
use App\Admin\Actions\Grid\Tools\GenerateAgreement;
use App\Admin\Repositories\Project;
use App\Admin\Repositories\ProjectAgreement;
use App\Models\Project as ModelsProject;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProjectAgreementController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProjectAgreement(), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->disableDeleteButton();
            $grid->disablePerPages();
            $grid->paginate(10);

            $currProjectID = request()->input('project_id', 0);
            $projectModel = ModelsProject::find($currProjectID);

            if (!Admin::user()->can('batch-delete-expert-agreement')) {
                $grid->disableRowSelector();
                $grid->disableBatchDelete();
            }

            $grid->scrollbarX();

            /**
             * 按钮控制.
             */
            // 生成协议
            if ($projectModel->update_info_end == 0) {
                if (Admin::user()->can('create-agreement')) {
                    $grid->tools([
                        new GenerateAgreement()
                    ]);
                }
            }

            $grid->column('id');
            $grid->column('agreement_code');
            $grid->column('user_auth_name', '收款人姓名');
            $grid->column('bank_card');
            $grid->column('bank_name');
            $grid->column('status')->using([0 => '待审核', 1 => '审核通过', -1 => '驳回', -2 => '终止'])->label([
                0 => 'primary',
                -2 => 'danger',
                1 => 'success',
                -1 => Admin::color()->info(),
            ]);
            $grid->column('role')->display(function ($roleId) {
                $role = Project::getFeeTypeOptions();

                return isset($role[$roleId]) ? $role[$roleId] : '';
            })->label();
            $grid->column('fee_type_key', '费用类型')->using(Project::getFeeTypeOptions()->toArray())->label();

            $grid->column('fee_type_value', '金额')->display(function ($value) {
                if ($this->before_after_tax == 1) {
                    return '<span style="color:blue">税后金额：' . $value . '</span>';
                } elseif ($this->before_after_tax == 2) {
                    return '<span style="color:red">税前金额：' . $value . '</span>';
                }
            });


            $grid->column('活动时间')->display(function () {
                return $this->active_date . '<br/>' . $this->active_time_begin . '-' . $this->active_time_end . '<br/>' . $this->active_week;
            });
            $grid->column('agreement_file_sign', '签字后协议')->display(function ($pictures) {
                return explode(',', $pictures);
            })->image('', 100, 100);
            $grid->column('express_name', '快递单号')->display(function () {
                return $this->express_name . '<br/>' . $this->express_code;
            });

            $grid->column('audit_first', '协议审核状态')
                ->using(ProjectAgreement::getAuditStatus('audit_first'))
                ->label([1 => 'success', '-1' => 'danger']);
            $grid->column('audit_fee', '劳务审核状态')
                ->using(ProjectAgreement::getAuditStatus('audit_fee'))
                ->label([1 => 'success', '-1' => 'danger']);


            $grid->column('created_at');

            $grid->actions(function (Grid\Displayers\Actions $actions) use ($projectModel) {

                // 审批流(1=协议审核,2=劳务审核)
                // if ($this->audit_global_status == 1 && Admin::user()->can('audit-first') && $this->status <> -2) {
                //     $actions->append(new AuditAction('协议审核'));
                // }

                // if ($this->audit_global_status == 2 && Admin::user()->can('audit-fee') && $this->status <> -2) {
                //     $actions->append(new AuditAction('劳务审核'));
                // }
                if ($this->status != -2) {
                    if ($this->audit_first == -1 || $this->audit_fee == -1) {
                        if (Admin::user()->can('check-agreement-audit-view')) {
                            $actions->append(new AuditRemarkAction()); // 审核意见
                        }
                    } else {

                        if ($this->status == 1) {
                            if (Admin::user()->can('download-agreement')) {
                                $actions->append('<a href="/download?cmd=agreement' . '&agreement_id=' . $actions->row->id . '" class="text-custom" style="margin-right: 10px;"><i class="fa fa-download"></i>下载协议</a>');
                            }
                        } else {
                            if (Admin::user()->can('download-agreement')) {
                                $actions->append('<a href="/download?cmd=agreement' . '&agreement_id=' . $actions->row->id . '" class="text-custom" style="margin-right: 10px;"><i class="fa fa-download"></i>下载协议</a>');
                            }
                            if ($projectModel->update_info_end == 0) {
                                if (Admin::user()->can('upload-agreement')) {
                                    $actions->append(new UploadAgreementAction('上传协议')); // 上传协议
                                }
                                // $actions->append(new AgreementAuditAction()); // 审核信息
                                if (Admin::user()->can('create-express')) {
                                    $actions->append(new ExpressAction('快递单号'));
                                }
                            }
                        }
                    }
                }

                // if (Admin::user()->isAdministrator()) {
                //     $actions->append(new AgreementStopAction()); // 中止
                // }
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('agreement_code');
                $filter->like('user_auth_name', '收款人姓名');
                $filter->like('role');
                $filter->equal('fee_type_key')->select(Project::getFeeTypeOptions());
                $filter->equal('audit_first')->select(ProjectAgreement::getAuditStatus('audit_first'));
                $filter->equal('audit_fee')->select(ProjectAgreement::getAuditStatus('audit_fee'));
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProjectAgreement(), function (Show $show) {
            $show->field('id');
            $show->field('project_id');
            $show->field('user_id');
            $show->field('agreement_code');
            $show->field('user_auth_name');
            $show->field('bank_card');
            $show->field('bank_username');
            $show->field('bank_name');
            $show->field('bank_addr');
            $show->field('id_card');
            $show->field('mobile');
            $show->field('company');
            $show->field('fee_type_key');
            $show->field('fee_type_value');
            $show->field('role');
            $show->field('active_date');
            $show->field('active_week');
            $show->field('active_time_begin');
            $show->field('active_time_end');
            $show->field('express_name');
            $show->field('express_code');
            $show->field('status');
            $show->field('pay_status');
            $show->field('audit_global_status');
            $show->field('audit_remark');
            $show->field('audit_fee');
            $show->field('audit_first');
            $show->field('audit_second');
            $show->field('audit_final');
            $show->field('audit_log');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProjectAgreement(), function (Form $form) {
            $form->display('id');
            $form->text('project_id');
            $form->text('user_id');
            $form->text('agreement_code');
            $form->text('user_auth_name');
            $form->text('bank_card');
            $form->text('bank_username');
            $form->text('bank_name');
            $form->text('bank_addr');
            $form->text('id_card');
            $form->text('mobile');
            $form->text('company');
            $form->text('fee_type_key');
            $form->text('fee_type_value');
            $form->text('role');
            $form->text('active_date');
            $form->text('active_week');
            $form->text('active_time_begin');
            $form->text('active_time_end');
            $form->text('express_name');
            $form->text('express_code');
            $form->text('status');
            $form->text('pay_status');
            $form->text('audit_global_status');
            $form->text('audit_remark');
            $form->text('audit_fee');
            $form->text('audit_first');
            $form->text('audit_second');
            $form->text('audit_final');
            $form->text('audit_log');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
